
Ext.define('DirectApp.controller.DirectCtrl', {
	
	extend : 'Ext.app.Controller',
	models : ['DirectModel'],
	views : ['DirectGrid', 'DirectToolbar', 'DirectForm'],
	stores : ['DirectStore'],
	
	refs : [{
		ref : 'personalForm',
		selector : '#personalForm'
	},{
		ref : 'directGrid',
		selector : '#directGrid'
	}],
	
	init : function(){
		this.control({
			'panel form button[action="savePersonal"]' : {
				click : this.onSavePersonalButtonClick,
				scope : this
			},
			
			'panel form button[action="cancelPersonal"]' : {
				click : this.onCancelPersonalButtonClick,
				scope : this
			},
			
			'grid' : {							
				itemcontextmenu : this.onGridItemContextMenu,
				scope : this
			}
			
		});		
	},

	onSavePersonalButtonClick : function(btn, e){		
		this.getForm().submit();
	},
	
	onCancelPersonalButtonClick : function(btn, e){
		this.getForm().reset();
	},

	onGridItemContextMenu : function(grid, record, item, index, e){
		e.stopEvent();
		var contextMenu = Ext.create('Ext.menu.Menu', {
			items : [{
				iconCls : 'silk-edit',
				text : '<b>Edit</b>',
				handler : function(){
					gCmp('firstName').setValue(record.get('firstName'));
					gCmp('lastName').setValue(record.get('lastName'));
					gCmp('company').setValue(record.get('company'));
					gCmp('email').setValue(record.get('email'));
					gCmp('dob').setValue(record.get('dob'));
					gCmp('age').setValue(record.get('age'));
					gCmp('coworker').setValue(record.get('coworker'));
				}
			},{
				iconCls : 'silk-delete',
				text : '<b>Delete</b>',
				handler : function(){
					var selectedRecords = grid.getSelectionModel().getSelection();
					grid.getStore().remove(selectedRecords);
					grid.getStore().sync();										
				}
			}]
		});
		contextMenu.showAt(e.getXY());	
	},
	
	getForm : function(){
		return this.getPersonalForm().getForm();
	}
	
});
